package com.tanhua.admin.service;

import cn.hutool.core.date.DateUtil;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.vo.CumulativeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DashBoardService {

    @Autowired
    AnalysisMapper analysisMapper;

    @DubboReference
    UserApi userApi;


    public CumulativeVo summary() {
        //封装返回数据
        CumulativeVo vo = new CumulativeVo();
        //累计用户
        Integer cumulativeUsers = userApi.cumulativeUsers();
        vo.setCumulativeUsers(cumulativeUsers);
        //过去30天活跃用户
        String todayStr = DateUtil.today();
        Date today = DateUtil.parse(todayStr);

        Date monthDate = DateUtil.offsetDay(today,-30);
        Integer activePassMonth = analysisMapper.activePass(monthDate);
        vo.setActivePassMonth(activePassMonth);
        //过去7天活跃用户
        Date weekDate = DateUtil.offsetDay(today,-7);
        Integer activePassWeek = analysisMapper.activePass(weekDate);
        vo.setActivePassWeek(activePassWeek);
        //今日新增用户
        Integer newUsersToday = analysisMapper.newUsers(today);
        vo.setNewUsersToday(newUsersToday);
        //今日新增用户涨跌率
        Date  yesterday = DateUtil.offsetDay(today,-1);
        Integer newUsersYesterday = analysisMapper.newUsers(yesterday);
        if (newUsersYesterday != null && newUsersYesterday != 0) {
            Integer usersTodayRate = (newUsersToday - newUsersYesterday)*100/newUsersYesterday;
            vo.setNewUsersTodayRate(usersTodayRate);
        }
        //今日登陆次数
        Integer loginTimesToday = analysisMapper.loginTimes(today);
        vo.setLoginTimesToday(loginTimesToday);
        //今日登陆次数涨跌率
        Integer loginTimesYesterday = analysisMapper.loginTimes(yesterday);
        if (loginTimesYesterday != null && loginTimesYesterday != 0){
            Integer loginTimesTodayRate = (loginTimesToday - loginTimesYesterday)*100/loginTimesYesterday;
            vo.setLoginTimesTodayRate(loginTimesTodayRate);
        }
        //今日活跃用户
        Integer activeUsersToday = analysisMapper.activeUsers(today);
        vo.setActiveUsersToday(activeUsersToday);
        //今日活跃用户跌涨率
        Integer activeUsersYesterday =analysisMapper.activeUsers(yesterday);
        if (activeUsersYesterday != null && activeUsersYesterday != 0) {
            Integer activeUsersTodayRate = (activeUsersToday - activeUsersYesterday)*100/activeUsersYesterday;
            vo.setActiveUsersTodayRate(activeUsersTodayRate);
        }
        return vo;
    }
}
